namespace BEGDC.CC.Web.UserControls.ECS
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.Collections;
	using BEGDC.CC.BL.CCFlow.ECS;
	using BEGDC.CC.Entity;
	using BEGDC.CC.BL.Workflow;
	using BEGDC.CC.BL.Common;
	using BEGDC.Utility.Castle;
	using BEGDC.CC.BL.Common.Query;

	/// <summary>
	///		Summary description for DeptVPApprove.
	/// </summary>
	public class ECSHeadApproval : BaseECS
	{
		#region Variables
		protected BEGDC.CC.Web.UserControls.ECS.ECSCtrl ECSCtrl;
		protected System.Web.UI.WebControls.ImageButton Btn_Submit;
		protected System.Web.UI.WebControls.ImageButton Btn_Query;
		protected System.Web.UI.WebControls.TextBox tbOperationFlag;
		protected System.Web.UI.WebControls.TextBox tbOperationContent;
		protected System.Web.UI.WebControls.TextBox tbComment;
		protected System.Web.UI.WebControls.ImageButton Btn_Reject;
		protected System.Web.UI.HtmlControls.HtmlTableRow TR_title;
		protected System.Web.UI.WebControls.ImageButton Btn_Reply;
		protected BEGDC.CC.Web.Common.StampCtrl StampCtrl1;
		protected System.Web.UI.WebControls.Label lbAppNo;
		protected BEGDC.CC.Web.UserControls.CV.CVPrintCtrl cvPrintCtrl;
		#endregion

		#region Properties
		private bool IsQuery
		{
			get
			{
				string state = this.Request.QueryString["State"];
				if(state == null) return false;
				return state == SysDicItem.ProjectStatusFlag.QUERY;
			}
		}

		private bool IsView
		{
			get
			{
				string state = this.Request.QueryString["State"];
				if(state == null) return false;
				if(state == SysDicItem.ProjectStatusFlag.View || state == SysDicItem.ProjectStatusFlag.CANCEL
					|| state == SysDicItem.ProjectStatusFlag.Pending || state == SysDicItem.ProjectStatusFlag.End
					|| state == SysDicItem.ProjectStatusFlag.Participation)
					return true;
				else
					return false;
			}
		}
		protected string Step
		{
			get
			{
				IQuery queryService = BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.QueryService] as IQuery;
				FlowInstance flowInstance = queryService.QueryFlowInstance( this.AppID );
				if( flowInstance == null )
					return "Close";
				else
					return flowInstance.CurrentActiveStep.StepCode;
			}
		}
		#endregion

		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!this.IsPostBack)
			{
				base.PreInitForm();

				this.Btn_Submit.Attributes["onclick"] = "javascript:OnSubmit();";
				this.Btn_Query.Attributes.Add("onclick", "javascript:OpenQueryPane("+Request.QueryString["AppID"]+");");
				this.Btn_Reject.Attributes.Add("onclick", "javascript:OpenCommentPane();");
				this.Btn_Reply.Attributes.Add("onclick", "javascript:OpenReplyPane("+Request.QueryString["FlowID"]+");");
				
				InitForm();

				if(this.IsView)
				{
					this.TR_title.Visible = false;
				}

				if( (this.Step != BEGDC.CC.BL.Workflow.WorkTransition.ECS_ECSHeadApproval && !this.IsQuery)
					|| Request.Url.AbsoluteUri.IndexOf("workflowview") >= 0)
				{
					this.Btn_Reply.Visible = false;
					this.Btn_Query.Visible = false;
					this.Btn_Reject.Visible = false;
					this.Btn_Submit.Visible = false;
				}
			}
			ButtonStatusSetting();
		}

		#region ControlStatusSetting
		private void ControlStatusSetting(Accessibility access )
		{
			if( access == Accessibility.Readonly )
			{
				this.DisableChildrenControls(this);
				//this.stampCtrl1.Visible = false;
			}
		}
		#endregion

		#region InitForm
		private void InitForm()
		{
			bool isReadOnly = true;
			long flowId  = this.LoadApplication();

			if(this.Application!=null)
			{
				if(this.Application.AppNo != string.Empty)
					this.lbAppNo.Text = "( "+this.Application.AppNo+" )";

				ECSCtrl.isCoveredPerson = !this.Application.IsTransition;
				ECSCtrl.isReadOnly = isReadOnly;
				ECSCtrl.DisableEditECS();
			}
			else
			{
				WebUtility.Go2ErrorPage( this.Page, "wrong Application ID" );
			}
		}
		#endregion

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Btn_Reply.Click += new System.Web.UI.ImageClickEventHandler(this.Btn_Reply_Click);
			this.Btn_Query.Click += new System.Web.UI.ImageClickEventHandler(this.Btn_Query_Click);
			this.Btn_Reject.Click += new System.Web.UI.ImageClickEventHandler(this.Btn_Reject_Click);
			this.Btn_Submit.Click += new System.Web.UI.ImageClickEventHandler(this.Btn_Submit_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		#region BuildObjects

		private BEGDC.CC.Entity.Application BuildApplication()
		{
			IECS ECSService = AppContainer.Instance[ServiceConst.ECSService] as IECS;
			BEGDC.CC.Entity.Application App = ECSService.GetApplication(this.AppID);
			return App;
		}

		#endregion

		#region Submit
		private void Btn_Submit_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				BEGDC.CC.Entity.Application App = BuildApplication();
				IECS ECSService = AppContainer.Instance[ServiceConst.ECSService] as IECS;

				ECSService.SubmitEVPApproval(App,this.tbComment.Text);

				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
			}
			catch(Exception exp)
			{
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM002 + "');window.close();</script>");
			}
		}
		#endregion

		#region Reject
		private void Btn_Reject_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				IECS ECSService = AppContainer.Instance[ServiceConst.ECSService] as IECS;
				ECSService.RejectEVPApproval(Convert.ToInt64(Request.QueryString["FlowID"]), WorkTransition.Reject, this.tbComment.Text);

				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
			}
			catch(Exception exp)
			{
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM002 + "');window.close();</script>");
			}
		}
		#endregion

		#region Query
		private void Btn_Query_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				if(this.tbOperationFlag.Text == "query")
				{
					IECS ECSService = AppContainer.Instance[ServiceConst.ECSService] as IECS;
					ECSService.Query(Convert.ToInt64(Request.QueryString["FlowID"]), this.tbOperationContent.Text, Session["Comment"].ToString());

					Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
				}
			}
			catch(Exception exp)
			{
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM002 + "');window.close();</script>");
			}
		}
		#endregion

		#region Export
		private void Btn_Export_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				ExcelTool.ICCExcelExport excelExport;
				if (this.Application.IsTransition)
					excelExport=new ExcelTool.ExportCRFormA();
				else
					excelExport=new ExcelTool.ExportCRFormB();
				excelExport.Export(this.Page, this.AppID );
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');</script>");

			}
			catch (BEGDC.Utility.ExceptionManagement.GDCException exp)
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.ErrorMessage.Export_0001 + "');</script>");
			}
		}
		#endregion

		#region ButtonStatusSetting
		protected override void ButtonStatusSetting()
		{			
			if( this.Access == null )
				return;

			this.Btn_Submit.Visible = this.Access.IsButtonVisible(ECSTransitionList.Approve);
			//this.Btn_Query.Visible = this.Access.IsButtonVisible(ECSTransitionList.Query);
			this.Btn_Query.Visible = false;
			this.Btn_Reject.Visible = this.Access.IsButtonVisible(ECSTransitionList.Reject);
			this.Btn_Reply.Visible = this.Access.IsButtonVisible(ECSTransitionList.Reply);
			this.cvPrintCtrl.ProcessType = WorkflowTypes.ECS;
			this.cvPrintCtrl.Visible = this.Access.IsButtonVisible(ECSTransitionList.Copy);
		}
		#endregion

		#region Reply
		private void Btn_Reply_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				IECS ECSService = AppContainer.Instance[ServiceConst.ECSService] as IECS;

				ECSService.ReplyQuery(Convert.ToInt64(Request.QueryString["FlowID"]),WebUtility.DeCodeString(Server, tbComment.Text));
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
			}
			catch(Exception exp)
			{
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM002 + "');window.close();</script>");
			}
		}
		#endregion

		//Should move to another page in a later time***********************************************!!!!
		#region Modify
		public void ShowModifyStatus()
		{
			if(this.Application!=null)
			{
				ECSCtrl.ShowModifyStatus();
			}
		}

		public void EndModifyStatus()
		{
			if(this.Application!=null)
			{
				ECSCtrl.EndModifyStatus();
			}
		}

		public void Modify()
		{
			IECS ECSService = AppContainer.Instance[ServiceConst.ECSService] as IECS;
			BEGDC.CC.Entity.Application App = ECSService.GetApplication(this.AppID);
			if(App!=null)
			{
				IList customerAttendee = new ArrayList();
				IList alAttendee = new ArrayList();
				ECSCtrl.BuildModificationChange(ref App,ref customerAttendee,ref alAttendee);
				ECSService.SaveEZModification(App,customerAttendee,alAttendee);
			}
		}
		#endregion
	}
}
